package com.fmsz.xmly.fragments.findfgts;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;

import com.fmsz.xmly.R;
import com.fmsz.xmly.adapters.broadcast.ProvinceLvAdapter;
import com.fmsz.xmly.application.MyApplication;
import com.fmsz.xmly.callbacks.FindBroadHttpService;
import com.fmsz.xmly.entitys.broadcast.province.RadioStationEntity;
import com.fmsz.xmly.widget.SlidingFinshLayout;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class BroadProvinceBaseFgt extends Fragment implements Callback<RadioStationEntity>, AbsListView.OnScrollListener {


    private String provinceCode;
    private ListView listView;
    private List<RadioStationEntity.ResultEntity> datas;
    private ProvinceLvAdapter lvAdapter;
    private SlidingFinshLayout layout;
    private boolean loadFinsh;
    private int pageNum = 1;
    private boolean isBottom;
    private FindBroadHttpService broadService;

    public BroadProvinceBaseFgt() {
        // Required empty public constructor
    }

    public static BroadProvinceBaseFgt newInstance(String provinceCode) {
        Bundle args = new Bundle();
        args.putString("provinceCode",provinceCode);
        BroadProvinceBaseFgt fragment = new BroadProvinceBaseFgt();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_broad_province_base_fgt, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle bundle = getArguments();
        provinceCode = bundle.getString("provinceCode");
        initView(view);
        initData();
    }

    private void initData() {
        MyApplication application = (MyApplication) getActivity().getApplication();
        broadService = application.getBroadService();
        broadService.getProvinceListData(provinceCode,"2",pageNum).enqueue(this);
    }

    private void initView(View view) {
        listView = ((ListView) view.findViewById(R.id.broad_province_lv));
        lvAdapter = new ProvinceLvAdapter(getContext());
        listView.setAdapter(lvAdapter);
        listView.setOnScrollListener(this);
    }

    @Override
    public void onResponse(Call<RadioStationEntity> call, Response<RadioStationEntity> response) {
        if (response.body()!=null) {
            if (pageNum!=1) {
                loadFinsh = true;
            }
            lvAdapter.addAll(response.body().getResult());
        }
    }

    @Override
    public void onFailure(Call<RadioStationEntity> call, Throwable t) {

    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (isBottom && scrollState==SCROLL_STATE_IDLE && !loadFinsh) {
            broadService.getProvinceListData(provinceCode,"2",++pageNum).enqueue(this);
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (firstVisibleItem+visibleItemCount==totalItemCount) {
            isBottom = true;
        }else{
            isBottom = false;
        }
    }
}
